﻿/*-------------------------------------------------------------------------
* 命名空间: SiliconSelect.Common/CalcDetectedLevel
* 类       名: CalcDetectedLevel
* 功      能:  TD
* 时      间:  2023/7/13 11:39:42
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using SiliconSelect.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SiliconSelect.Common
{
    public class CalcDetectedLevel
    {
        /// <summary>
        /// 根据电阻率的检测结果去评定电阻率的等级,将结果保存到全局变量中即可.
        /// </summary>
        /// <param name="data">数据,是一个List<string> 类型,里面存放的是平均电阻,还有PN型</param>
        public static void CalcResistivityLevel(List<string> data)
        {
            string levelVal = "3"; // 这里实际的时候是要去计算的.根据等级规则去计算,但是这里只是直接给出了结果
            LogHelper.WriteLog(LogType.Info, $"使用平均电阻率的值 {data[0]}, 计算其对应等级为 {levelVal}");
            GlobalValues.ResistivityDetectedMap[GlobalValues.WaferIdOfResistivity] = levelVal;
        }

        /// <summary>
        /// 根据值和对应的规则去获取等级.目前是随机数等级,从1到8
        /// </summary>
        /// <param name="lengthMaxRule"></param>
        /// <param name="lengthMax"></param>
        /// <returns></returns>
        public static List<int> CalcDetectedLevelByRule(Dictionary<int, string> detectedRule, string detectedVal)
        {
            List<int> result = new List<int>();
            try
            {
                // 如果lengthMaxRule的长度为0,就证明这条数据不检测
                if (detectedRule.Count == 0)
                {
                    return result;
                }
                // 如果有检测规则,就去判断检测的等级(注意如果是空的并且小于等于开放的检测等级个数,就认为它也满足当前的等级.
                // 求detectedRule 和 GlobalValues.NormalDetectedTotalCounts的最小值
                var loopCount = Math.Max(GlobalValues.NormalDetectedTotalCounts, detectedRule.Count);
                for (int i = 1; i <= loopCount; i++)
                {
                    var detectedRuleEverVal = detectedRule[i];
                    if (detectedRuleEverVal == string.Empty)
                    {
                        result.Add(i);
                        continue; // 继续下一下循环
                    }
                    if (!detectedRuleEverVal.Contains(","))
                    {
                        LogHelper.WriteLog(LogType.Fatal, $"规则错误,错误的规则内容为:{detectedRuleEverVal},规则里面没有逗号!");
                        continue;
                    }
                    var detectedBetween = detectedRuleEverVal.Split(',').Select(double.Parse).ToList();
                    if (detectedBetween.Count != 2)
                    {
                        LogHelper.WriteLog(LogType.Fatal, $"规则错误,错误的规则内容为:{detectedBetween},属于的规则:{detectedRule[i]}");
                        continue;
                    }
                    else
                    {
                        double detectedValDouble = double.Parse(detectedVal);
                        if (detectedBetween[0] <= detectedValDouble && detectedValDouble <= detectedBetween[1])
                        {
                            result.Add(i);
                        }
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogType.Exception, ExceptionHelper.GetFullException(ex, MethodBase.GetCurrentMethod()!));
                return result;
            }
        }
    }
}
